package tech.bmatch.meituan.service.impl;

import org.slf4j.LoggerFactory;
import tech.bmatch.meituan.model.Dish;
import tech.bmatch.meituan.model.Merchant;
import tech.bmatch.meituan.model.MerchantSearchParam;
import tech.bmatch.meituan.util.DistanceUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: S1P1FinalProject
 * @description: 服务实现
 * @author: Awake
 * @create: 2018-10-03 10:33
 **/
public class MerchantServiceImpl extends MerchantFileStoreServiceImpl {
    public static final org.slf4j.Logger logger = LoggerFactory.getLogger(MerchantServiceImpl.class);
    private Map<String, Merchant> merchants;

    /**
     * 添加一个商家
     *
     * @param merchant
     */
    @Override
    public void addMerchant(Merchant merchant) {
        //判断merchants是不是空的，是的话就创建新的
        if (merchants == null) {
            merchants = new HashMap<String, Merchant>();
        }
        //判断传入的商家参数是否为空
        if (merchant == null) {
            return;
        }
        merchants.put(merchant.getId(), merchant);

    }

    /**
     * 添加一个菜品到商家
     *
     * @param dish
     */
    @Override
    public void addDish(Dish dish, String id) {
        if (dish == null) {
            return;
        }

        if (!merchants.containsKey(id)) {
            logger.error("没有找到id为" + id + "的商家");
            return;
        }

        Merchant updateMerchant = merchants.get(id);
        if (updateMerchant.getDishes() == null) {
            updateMerchant.setDishes(new ArrayList<>());
        }
        updateMerchant.getDishes().add(dish);
        System.out.println("添加成功");
    }

    @Override
    public List<Dish> findDishesByMerchantId(String id) {
        if (!merchants.containsKey(id)) {
            logger.error("没有找到id为" + id + "的商家");
            return null;
        }
        return merchants.get(id).getDishes();
    }

    /**
     * @param param 商家查询传入必要参数
     * @return 商家列表
     */
    @Override
    public List<Merchant> search(MerchantSearchParam param) {
        //定义一个要生成的商家列表
        List<Merchant> merchantList = new ArrayList<>();
        for (Map.Entry<String, Merchant> merchantEntry :
                merchants.entrySet()) {
            Merchant merchant = merchantEntry.getValue();
            Double distance = DistanceUtil.getDistance(param.getLon(), param.getLat(), merchant.getLon(), merchant.getLat());
            merchant.setDistance(distance);
            merchantList.add(merchant);
        }

        Stream<Merchant> stream = merchantList.stream();
        if (param.getName() != null && !param.getName().equals("")) {
            stream = stream.filter(merchant -> merchant.getName().indexOf(param.getName()) >= 0);
        }
        stream = stream.sorted(Comparator.comparingInt(m -> ((Double) m.getDistance()).intValue()));

        return stream.collect(Collectors.toList());
    }

    /**
     * 将商家信息进行整合保存，并调用文件服务中的store方法
     */
    public void storeMerchants() {
        //存储数据
        List<Merchant> merchantList = merchants.values().
                stream().collect(Collectors.toList());
        store(merchantList);
    }
}
